package com.github.beihaifeiwu.myspring.jackson.msgpack;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import org.msgpack.jackson.dataformat.MessagePackFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.web.codec.CodecCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.codec.CodecConfigurer;
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;

/**
 * Created by liupin on 2017/3/29.
 */
@Configuration
@ConditionalOnClass({ MessagePackFactory.class, ObjectMapper.class })
public class MessagePackConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public MessagePackMapper messagePackMapper() {
        MessagePackMapper messagePackMapper = new MessagePackMapper();
        messagePackMapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector());
        return messagePackMapper;
    }

    @Configuration
    @ConditionalOnClass(CodecConfigurer.class)
    static class MessagePackWebFluxConfiguration {
        @Bean
        public CodecCustomizer messagePackCodecCustomizer(MessagePackMapper messagePackMapper) {
            return configurer -> MessagePackCodecSupport.register(configurer, messagePackMapper);
        }
    }

    @Configuration
    @ConditionalOnWebApplication(type = Type.SERVLET)
    @ConditionalOnClass(AbstractJackson2HttpMessageConverter.class)
    static class MessagePackWebServletConfiguration {

        @Bean
        @ConditionalOnMissingBean
        public MappingJackson2MsgpackHttpMessageConverter messagePackConverter(MessagePackMapper messagePackMapper) {
            return new MappingJackson2MsgpackHttpMessageConverter(messagePackMapper);
        }

    }

}